package com.kuang.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kuang.common.anno.IgnoreToken;
import com.kuang.entity.Product;
import com.kuang.service.product.ProductServiceImpl;
import com.kuang.vo.ProductVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class ProductController extends BaseController {

    @Autowired
    private ProductServiceImpl productService;

    private static final String AUTH = "Authorization";
    private static final String AUTH_USERNAME = "ksdname";
    @GetMapping("/product/list")
    @IgnoreToken
  //  @AccessIpLimiter(limit = 5,timeout = 1)
    public Object index() {
        Page<Product> page = new Page<>(1, 5);
        return  productService.page(page,null);
    }

    @GetMapping("/product/save")
  //  @AccessIpLimiter(limit = 5,timeout = 1)
    public String save() {
        return "success";
    }

    @GetMapping("/product/findProductByPage")
    //  @AccessIpLimiter(limit = 5,timeout = 1)
    //@CheckLogin
    public Page<Product> findProductByPage(ProductVo productVo) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        HttpServletResponse response = requestAttributes.getResponse();
        String token = getParam(request, AUTH);
        String username = getParam(request, AUTH_USERNAME);
        return productService.findProductByPage(productVo);
    }
    public static String getParam(HttpServletRequest request, String filedName) {
        //1 :在参数里面区获取对应filedName的值
        String param = request.getParameter(filedName);
        // 2:如果不存在
        if (StringUtils.isEmpty(param)) {
            // 3: 就请求头区获取对应filedName的值
            param = request.getHeader(filedName);
        }
        return param;
    }

    @GetMapping("/product/index")
    //  @AccessIpLimiter(limit = 5,timeout = 1)
    //@CheckLogin
    public Page<Product> findIndex(int pageNo, int pageSize) {
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        HttpServletResponse response = requestAttributes.getResponse();
//        String token = getParam(request, AUTH);
//        String username = getParam(request, AUTH_USERNAME);
        return productService.findIndex(pageNo,pageSize);
    }

    @GetMapping("/product/detail/{productId}")
    @IgnoreToken
    public Product getProductById(@PathVariable("productId") int productId){
        return productService.getById(productId);
    }



}
